#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using sqengine.Engine.Common;
using sqengine.Components;
#endregion

namespace sqengine.PhysicObjects
{

    public class BulletObject : PhysicObject
    {      
        Game _game;
        bool _collisioned;
        PlayerStats _currentplayer;

        double _lifetime = 5000;

        public BulletObject(Game game, Model model, float radius, Matrix orientation, Vector3 position, float mass, string s_texture, PlayerStats player )
            : base(game, model)
        {
            _game = game;
            body = new Body();
            collision = new CollisionSkin(body);            
            collision.AddPrimitive(new Sphere(Vector3.Zero * 5.0f, radius), new MaterialProperties(0.2f, 1.2f, 1f));
            body.CollisionSkin = this.collision;
            Vector3 com = SetMass(0.1f);
            body.MoveTo(position + com, orientation);
            body.EnableBody();

            _currentplayer = player;

            this.scale = Vector3.One * radius;

            body.CollisionSkin.callbackFn += new CollisionCallbackFn(BulletCollision_callbackFn);
        }

        bool BulletCollision_callbackFn(CollisionSkin skin0, CollisionSkin skin1)
        {
            _collisioned = true;

            List<PK3_Model> botlist = ((sqengine)_game).GetBotList;

            for (int i = 0; i < botlist.Count; i++)
            {
                if (skin1 == botlist[i].PhysicsBody.CollisionSkin)
                {
                    if (!botlist[i].JustHit)
                    {
                        botlist[i].BotPlayer.Hit(_currentplayer.Weapon.WeaponDamage);
                        botlist[i].JustHit = true;
                        break;
                    }
                }

            }
            
            return true;
        }

        public override void ApplyEffects(BasicEffect effect)
        {
            effect.DiffuseColor = Color.Gold.ToVector3();
            effect.DirectionalLight0.Enabled = true;
            effect.DirectionalLight0.DiffuseColor = Color.BlueViolet.ToVector3();
            effect.DirectionalLight0.Direction = body.Position;

        }

        public override void Update(GameTime gameTime)
        {
            if (_collisioned)
            {
                _lifetime -= gameTime.ElapsedGameTime.TotalMilliseconds;

                if (_lifetime <= 0)
                {
                    _game.Components.Remove(this);
                }
            }
            base.Update(gameTime);
        }

    }
}
